package com.fintech.modules.demo;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fintech.modules.opensearch.SearchBiz;

@RestController
@Configuration
@RequestMapping("/api/demo")
public class DemoController {
    private  static Logger logger = LoggerFactory.getLogger(DemoController.class);

    @Autowired
    @Qualifier("com.fintech.modules.demo.ObtainUUIDService")
    private ObtainUUIDService biz;

    @Value("${uuid.url}")
    private String uuidURL;
    @Value("${excel.path}")
    private String excelPath;
    
    @Value("${excel.count}")
    private String excelCount;
    
    @Autowired
    private SearchBiz searchBiz ;
    //  使用ribbon 实现负载均衡
    /*@RequestMapping(value = "/uuid", method = RequestMethod.GET)
    public String uuid() {
        String result = "UUID-ERROR";
        try {
            
            result = biz.getUUID(uuidURL,"CHEN");
        } catch (Exception e) {
            logger.error("===========helloSome error",e);
        }
        
        return result;
    }*/
    @RequestMapping(value = "/reload", method = RequestMethod.GET)
    public String reload() {
        String result = "GOOD";
        int start = 1;
        if(StringUtils.isNoneEmpty(excelCount)){
        	start = Integer.parseInt(excelCount);
        }
        for(int k = start ;k < 1000 ;k ++){
        	SearchBiz.flag2 = true;
        	SearchBiz.flag = true;
        	SearchBiz.excelList = new ArrayList<String>();
        	try {
            	StringBuffer sb =new StringBuffer("[");
            	String path = "E:\\data\\";
            	if(StringUtils.isNotEmpty(excelPath)){
            		path = excelPath;
            	}
            	//String path = "E:\\web\\2018年图片处理结果-俊峰\\";
                String data = searchBiz.parseAllExcelToStr(path,sb,k * 10).append("]").toString();
                //logger.info("===data:{}",data);
                if(StringUtils.isNotEmpty(data)){
                	JSONArray list = JSONObject.parseArray(data);
                	int size = list.size();
                	
                	if(size > 100){
                		for(int i = 0;i <= size / 100 ;i ++){
                			if((i+1) * 100 < size){
                				List tmpList = list.subList(i * 100, (i+1) * 100);
                				searchBiz.uploadBatchData(tmpList.toString());
                			}else{
                				List tmpList =  list.subList(i * 100, size);
                				searchBiz.uploadBatchData(tmpList.toString());
                			}
                		}
                	}else{
                		searchBiz.uploadBatchData(data);
                	}
                }else{
                    result ="import data is null!!!";
                }
            } catch (Exception e) {
                result="ERROR!!!";
                logger.error("===========reload error",e);
            }
        	
        	
        }
        
        logger.info(result);
        return result;
    }
    
    
}
